package com.yikaiye.android.yikaiye.ui.activity.home.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.model.ProductDataModel;
import com.yikaiye.android.yikaiye.model.ProductModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchViewModel extends BaseViewModel {

    public MutableLiveData<List<ProductModel>> productData = new MutableLiveData<>();

    /**
     * 搜索
     *
     * @param keyWord     关键字
     * @param isRecommend 是否是推荐产品
     */
    public void getProductList(String keyWord, boolean isRecommend) {
        Map<String, Object> map = new HashMap<>();
        map.put("page",1);
        map.put("",50);
        if (!isRecommend) {
            map.put("selCond",keyWord);
        }
        addSubscribe(mRetrofitHelper.getProductList(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<ProductDataModel>() {

            @Override
            public void onSuccess(BaseModel<ProductDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode() && baseModel.getData() != null) {
                    productData.setValue(baseModel.getData().getRecords());
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }
}
